﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;

namespace DrawingScreen.Objects
{
    public class CEllipse: CDrawingObject
    {
        public int XI;
        public int YI;
        public int X;
        public int Y;

        public Pen pen = new Pen(Color.Black);

        public int x;
        public int y;
        public int w;
        public int h;

        public void InitOtherParameters()
        { 
            int dx = System.Math.Abs(XI - X);
            int dy = System.Math.Abs(YI - Y);
            
            x = XI - dx;
            y = YI - dy;
            w = dx + dx;
            h = dy + dy;
        }

        public override void Draw(System.Drawing.Graphics g)
        {
            g.DrawEllipse(pen, x, y, w, h);
        }

        public static CEllipse CreateDrawingObject(XmlElement eleObject)
        {
            try
            {
                XmlElement eleEllipse = (XmlElement)eleObject.FirstChild;

                CEllipse ellipse = new CEllipse();
                ellipse.x = Int32.Parse(eleEllipse.Attributes["x"].Value);
                ellipse.y = Int32.Parse(eleEllipse.Attributes["y"].Value);
                ellipse.w = Int32.Parse(eleEllipse.Attributes["w"].Value);
                ellipse.h = Int32.Parse(eleEllipse.Attributes["h"].Value);

                Pen pen = Global.CreatePenFromXmlElement((XmlElement)eleEllipse.FirstChild);

                if (pen != null)
                {
                    ellipse.pen = pen;
                }

                return ellipse;

            }
            catch
            {
                return null;
            }
        }

        public override XmlNode convertToXmlNode(XmlDocument doc)
        {
            try
            {
                XmlElement eleObject = doc.CreateElement("OBJECT");

                XmlAttribute attObjectID = doc.CreateAttribute("id");
                attObjectID.Value = CDrawingObject.ELLIPSE_ID.ToString();
                eleObject.Attributes.Append(attObjectID);

                //////////////////////////////////////////////////////////////
                XmlElement eleEllipse = doc.CreateElement("ELLIPSE");

                XmlAttribute attX = doc.CreateAttribute("x");
                attX.Value = x.ToString();
                eleEllipse.Attributes.Append(attX);

                XmlAttribute attY = doc.CreateAttribute("y");
                attY.Value = y.ToString();
                eleEllipse.Attributes.Append(attY);

                XmlAttribute attW = doc.CreateAttribute("w");
                attW.Value = w.ToString();
                eleEllipse.Attributes.Append(attW);

                XmlAttribute attH = doc.CreateAttribute("h");
                attH.Value = h.ToString();
                eleEllipse.Attributes.Append(attH);

                XmlElement elePen = Global.CreateXmlElementFromPen(doc, pen);
                eleEllipse.AppendChild(elePen);

                //////////////////////////////////////////////////////////////

                eleObject.AppendChild(eleEllipse);

                return eleObject;
            }
            catch
            {
                return null;
            }
        }

      
    }
}
